package com.itboye.shop.me.adress;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.itboye.shop.R;
import com.itboye.shop.bean.CitysBean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;


/**
 *
 */
public class CityDBManager {

	SQLiteDatabase db;
	Context mContext;
	public static final String DB_NAME = "itboye.db";
	public static final String DB_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ "com.itboye.shop"; // 存放路径;
	private final int BUFFER_SIZE = 400000;
	private Cursor cursor;
	private String cityCode;

	public CityDBManager(Context mContext) {
		this.mContext = mContext;

	}

	/**
	 * 被调用方法
	 */
	public SQLiteDatabase openDateBase() {
		this.db = this.openDateBase(DB_PATH + "/" + DB_NAME);
		return this.db;
	}

	/**
	 * 被调用方法
	 */
	public void closeDateBase() {
		if (this.db != null)
			this.db.close();

	}

	/**
	 * 打开数据库
	 *
	 * @param dbFile
	 * @return SQLiteDatabase
	 * @author sy
	 */
	private SQLiteDatabase openDateBase(String dbFile) {
		File file = new File(dbFile);
		if (!file.exists()) {
			// // 打开raw中得数据库文件，获得stream流
			InputStream stream = mContext.getResources().openRawResource(
					R.raw.itboye);
			try {

				// 将获取到的stream 流写入道data中
				FileOutputStream outputStream = new FileOutputStream(dbFile);
				byte[] buffer = new byte[BUFFER_SIZE];
				int count = 0;
				while ((count = stream.read(buffer)) > 0) {
					outputStream.write(buffer, 0, count);
				}
				outputStream.close();
				stream.close();
				SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,
						null);
				return db;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return db;
	}

	/***
	 * 通过省的ID查找下面的市
	 * @param
	 * @return
	 */
	public ArrayList<CitysBean> queryCity(String provinceId){
		ArrayList<CitysBean> arrayCity=new ArrayList<>();
		db= SQLiteDatabase.openOrCreateDatabase(
				CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,
				null);
		if(this.db!=null)
			cursor = db.query("common_city", null, "father like ?", new String[]{provinceId+"%"}, null	,null, null);
		if (cursor != null)
			while(cursor.moveToNext()){
				CitysBean citysBean =new CitysBean();
				citysBean.setCityName(cursor.getString(cursor.getColumnIndexOrThrow("city")));
				citysBean.setCityId(cursor.getString(cursor.getColumnIndexOrThrow("cityID")));
				citysBean.setProvinceId(cursor.getString(cursor.getColumnIndexOrThrow("father")));
				arrayCity.add(citysBean);
			}
		cursor.close();
		db.close();
		return arrayCity;
	}

	/***
	 * 通过市的ID查找下面的区
	 * @param
	 * @return
	 */
	public ArrayList<CitysBean> queryArea(String cityId){
		ArrayList<CitysBean> arrayCity=new ArrayList<>();
		db= SQLiteDatabase.openOrCreateDatabase(
				CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,
				null);
		if(this.db!=null)
			cursor = db.query("common_area", null, "father like ?", new String[]{cityId+"%"}, null	,null, null);
		if (cursor != null)
			while(cursor.moveToNext()){
				CitysBean citysBean =new CitysBean();
				citysBean.setAreaName(cursor.getString(cursor.getColumnIndexOrThrow("area")));
				citysBean.setAreaId(cursor.getString(cursor.getColumnIndexOrThrow("areaID")));
				citysBean.setCityId(cursor.getString(cursor.getColumnIndexOrThrow("father")));
				arrayCity.add(citysBean);
			}
		cursor.close();
		db.close();
		return arrayCity;
	}

}
